package org.example.controller.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.example.dao.commom.Result;
import org.example.dao.commom.ResultCode;
import org.example.dao.entity.user.ULabel;
import org.example.dao.entity.user.UUserLabel;
import org.example.dao.vo.LabelVo;
import org.example.service.user.LabelServiceImpl;
import org.example.skytool.commom.ParamUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 22449
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class LabelController {

    @Resource
    private LabelServiceImpl labelService;


    //TODO 新增
    /**
     * 新增标签(管理)
     */
    @PostMapping("addLabel")
    public Result addLabel(@RequestBody ULabel label) {
        String name = label.getName();
        String background = label.getBackground();

        ParamUtil.StringParam(name, background);

        ULabel uLabel = labelService.addLabel(name, background);

        return new Result(ResultCode.SUCCESS, uLabel);
    }

    /**
     * 用户绑定标签(用户)
     */
    @PostMapping("addUserLabel")
    public Result addUserLabel(@RequestBody UUserLabel uUserLabel){

        Long labelId = uUserLabel.getLabelId();

        ParamUtil.LongParam(labelId);

        labelService.addUserLabel(labelId);

        return new Result(ResultCode.SUCCESS,null);
    }

    //TODO 查询

    /**
     * 查询可用标签(管理)(用户)
     */
    @PostMapping("selectLabel/{limit}/{start}")
    public Result selectLabel(@PathVariable("limit") Integer limit,
                              @PathVariable("start") Integer start) {

        IPage<ULabel> list = labelService.selectLabel(limit, start);

        return new Result(ResultCode.SUCCESS, list);
    }


    //TODO 更新
    /**
     * 更改标签颜色和名称(管理)
     */
    @PostMapping("updateLabel")
    public Result updateLabel(@RequestBody ULabel label) {
        Long id = label.getId();
        String name = label.getName();
        String background = label.getBackground();

        ParamUtil.StringParam(name, background);
        ParamUtil.LongParam(id);

        ULabel label1 = labelService.updateLabel(id, name, background);

        return new Result(ResultCode.SUCCESS, label1);
    }

    @PostMapping("updateUserLabel")
    public Result updateUserLabel(@RequestBody LabelVo labelVo){
        List<String> list = labelVo.getLabels();
        labelService.updateUserLabel(list);
        return new Result(ResultCode.SUCCESS,null);
    }



    //TODO 删除

    /**
     * 删除标签(管理)
     */
    @PostMapping("deleteLabel")
    public Result deleteLabel(@RequestBody ULabel label) {
        Long id = label.getId();

        ParamUtil.LongParam(id);

        labelService.deleteLabel(id);

        return new Result(ResultCode.SUCCESS, null);
    }

    /**
     * 用户解除绑定标签
     */
    @PostMapping("deleteUserLabel")
    public Result deleteUserLabel(@RequestBody UUserLabel uUserLabel){
        Long id = uUserLabel.getId();

        ParamUtil.LongParam(id);

        labelService.deleteUserLabel(id);

        return new Result(ResultCode.SUCCESS,null);
    }

}
